package com.znyj.cloud.device.dao;

import com.znyj.cloud.device.entity.TaskEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

/**
 * Created by gaoliwei02 on 17/6/5.
 */
@Repository
public interface TaskDao {
    public static final String SELECT_KEYS = " " +
            "id," +
            "user_id," +
            "device_id," +
            "order_id," +
            "create_time," +
            "duration_sec," +
            "send_status," +
            "control_msg," +
            "user_location," +
            "execute_result";
    public static final String INSERT_KEYS = " " +
            "user_id," +
            "device_id," +
            "order_id," +
            "create_time," +
            "duration_sec," +
            "send_status," +
            "control_msg," +
            "user_location," +
            "execute_result";
    public static final String INSERT_VALUES = " " +
            "#{userId}," +
            "#{deviceId}," +
            "#{orderId}," +
            "#{createTime}," +
            "#{durationSec}," +
            "#{sendStatus}," +
            "#{controlMsg}," +
            "#{userLocation}," +
            "#{executeResult}";
    public static final String SIMPLE_SELECT_KEYS = "id,device_id";

    @Insert("insert into cdb_task (" + INSERT_KEYS + ") values (" + INSERT_VALUES + ") ")
    public int insert(TaskEntity entity);

    @Update("update cdb_task set " +
            "user_id=#{userId}," +
            "order_id=#{orderId}" +
            "create_time=#{createTime}," +
            "duration_sec=#{durationSec}," +
            "send_status=#{sendStatus}," +
            "control_msg=#{controlMsg}," +
            "user_location=#{userLocation}," +
            "execute_result=#{executeResult} " +
            "where device_id=#{deviceId}")
    public int update(TaskEntity entity);

    @Select(" select " + SIMPLE_SELECT_KEYS + " from cdb_task where device_id=#{deviceId}")
    public TaskEntity findByDeviceId(@Param("deviceId") String deviceId);

    @Select(" select * from cdb_task where order_id=#{orderId}")
    public TaskEntity findByOrderId(@Param("orderId") long orderId);

    @Update("update cdb_task set " +
            "send_status=#{sendStatus}," +
            "execute_result=#{executeResult} " +
            "where order_id=#{orderId}")
    public int findByOrderIdAndUpdateStatusWithExecute(@Param("executeResult") String executeResult, @Param("orderId") long orderId, @Param("sendStatus") int sendStatus);

    @Update("update cdb_task set " +
            "send_status=#{sendStatus} " +
            "where order_id=#{orderId}")
    public int findByOrderIdAndUpdateStatus(@Param("orderId") long orderId, @Param("sendStatus") int sendStatus);


    @Select("select order_id from cdb_task order by id desc limit 1")
    public long findNewestOrderId();
}
